package com.yun.algorithmproblem.leetcode;

import java.util.Arrays;

public class Leetcode2779 {
    public static void main(String[] args) {
        int[] nums={4,6,1,2};
        int k=2;
        Leetcode2779 obj=new Leetcode2779();
        System.out.println(obj.maximumBeauty(nums, k));
    }

    //滑动窗口
    public int maximumBeauty(int[] nums, int k) {
        Arrays.sort(nums);
        int[] dp = new int[nums.length];
        dp[0] = 1;
        nums[0] += k;
        int topNum = 0;
        int max=1;
        for (int i = 1; i < nums.length; i++) {
            dp[i] = dp[i - 1] + 1;
            while (nums[i]-k > nums[topNum] && topNum < i) {
                dp[i]--;
                topNum++;
            }
            nums[i] += k;
            if(dp[i]>max){
                max=dp[i];
            }
        }
        return max;
    }

    //差分数组
}
